package com.jh3502.vivo.function.author;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;
import com.jh3502.vivo.utils.Utils;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AuthorListAdapter extends RecyclerView.Adapter<AuthorListAdapter.ViewHolder> implements View.OnClickListener {


    private Context mContext;
    private List<AuthorListBean> dataList;

    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private ButtonInterface viewClickDelete, viewClickSwitch;
    private boolean isEdit = false;

    public AuthorListAdapter(Context context, List<AuthorListBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }

    public void setButtonSetOnclickSwitch(ButtonInterface buttonInterface) {
        this.viewClickSwitch = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_author, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        if (isEdit) {
            viewHolder.itemDelete.setVisibility(View.VISIBLE);
        } else {
            viewHolder.itemDelete.setVisibility(View.GONE);
        }
        viewHolder.itemDelete.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickDelete != null) {
                viewClickDelete.onClick(position);
            }
        });
        viewHolder.itemUsed.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickSwitch != null) {
                viewClickSwitch.onClick(position);
            }
        });

        viewHolder.itemTitle.setText(dataList.get(position).getName());
        viewHolder.itemTitle.setText(dataList.get(position).getName());
        int status = dataList.get(position).getStatus();
        List<Map<String, Object>> list = Utils.getInstance().jsa2list(dataList.get(position).getPermissions());
        if (status == 0) {
            viewHolder.itemStatus.setVisibility(View.GONE);
            Glide.with(mContext).load(R.drawable.switches_off).into(viewHolder.itemUsed);

        } else if (status == 1) {
            viewHolder.itemStatus.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(R.drawable.switches_on).into(viewHolder.itemUsed);
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (Map<String, Object> map : list) {
            stringBuffer.append((String) map.get("name"));
            stringBuffer.append(" ");

        }
        viewHolder.itemCont.setText(stringBuffer.toString());
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public void setIsEdit(boolean b) {
        isEdit = b;
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_delete)
        ImageView itemDelete;
        @BindView(R.id.item_title)
        TextView itemTitle;
        @BindView(R.id.item_cont)
        TextView itemCont;
        @BindView(R.id.item_status)
        TextView itemStatus;
        @BindView(R.id.item_used)
        ImageView itemUsed;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}